package com.jugheadzhou.emrpoc.controller;

import com.jugheadzhou.emrpoc.mapper.MedicalRecordMapper;
import com.jugheadzhou.emrpoc.mapper.MedicalRecordTemplateMapper;
import com.jugheadzhou.emrpoc.model.MedicalRecord;
import com.jugheadzhou.emrpoc.model.MedicalRecordTemplate;
import com.jugheadzhou.emrpoc.service.MedicalRecordContentService;
import com.jugheadzhou.emrpoc.service.MedicalRecordTemplateService;
import com.jugheadzhou.emrpoc.vo.MedicalRecordContentVo;
import com.jugheadzhou.emrpoc.vo.MedicalRecordTemplateVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author jugheadzhou
 * @date 2021-10-24
 */
@Slf4j
@Controller
public class ViewController {

    @Resource
    private MedicalRecordMapper medicalRecordMapper;

    @Resource
    private MedicalRecordTemplateService medicalRecordTemplateService;

    @Resource
    private MedicalRecordContentService medicalRecordContentService;

    @Resource
    private MedicalRecordTemplateMapper medicalRecordTemplateMapper;

    @GetMapping("/")
    public String index(Model model) {
        model.addAttribute("now", LocalDateTime.now());
        return "index";
    }

    @GetMapping("/emr-list")
    public String emrList(Model model) {
        model.addAttribute("nav", "emr-list");
        List<MedicalRecord> medicalRecords = medicalRecordMapper.selectAll().stream().sorted(Comparator.comparing(MedicalRecord::getCreateTime).reversed()).collect(Collectors.toList());
        model.addAttribute("medicalRecords", medicalRecords);
        return "views/emr/emr-list";
    }

    @GetMapping("/emr-template-list")
    public String emrTemplateList(Model model) {
        model.addAttribute("nav", "emr-template-list");
        List<MedicalRecordTemplateVo> emrTemplateList = medicalRecordTemplateService.listByPublished();
        model.addAttribute("emrTemplateList", emrTemplateList);
        return "views/emr-template/emr-template-list";
    }

    @GetMapping("/new-emr-template")
    public String newEmrTemplate(Model model) {
        model.addAttribute("nav", "emr-template-list");
        return "views/emr-template/new-emr-template";
    }

    @GetMapping("/new-emr-list")
    public String newEmrList(Model model) {
        model.addAttribute("nav", "emr-list");
        List<MedicalRecordTemplateVo> emrTemplateList = medicalRecordTemplateService.listByPublished();
        model.addAttribute("emrTemplateList", emrTemplateList);
        return "views/emr/new-emr-list";
    }

    @GetMapping("/edit-emr-content")
    public String editEmrContent(String recordId, String templateId, Model model) {
        model.addAttribute("nav", "emr-list");
        MedicalRecordContentVo mrContent = medicalRecordContentService.info(recordId);
        model.addAttribute("mrContent", mrContent);
        MedicalRecordTemplate mrTemplate = medicalRecordTemplateMapper.selectByPrimaryKey(templateId);
        model.addAttribute("mrTemplate", mrTemplate);
        MedicalRecord medicalRecord = medicalRecordMapper.selectByPrimaryKey(recordId);
        model.addAttribute("medicalRecord", medicalRecord);
        return "views/emr/edit-emr-content";
    }

    @GetMapping("/roles")
    public String roles(Model model) {
        model.addAttribute("nav", "roles");
        return "views/system/roles";
    }

    @GetMapping("/users")
    public String users(Model model) {
        model.addAttribute("nav", "users");
        return "views/system/users";
    }

    @GetMapping("/new-role")
    public String newRole(Model model) {
        model.addAttribute("nav", "roles");
        return "views/system/new-role";
    }

    @GetMapping("/new-user")
    public String newUser(Model model) {
        model.addAttribute("nav", "users");
        return "views/system/new-user";
    }

    @GetMapping("/my-profile")
    public String myProfile(Model model) {
        model.addAttribute("nav", "my-profile");
        return "views/system/my-profile";
    }

}
